package cn.kuang.controller;

import cn.kuang.pojo.User;
import cn.kuang.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author 徽州大都督
 * @date 2020/7/22
 */
@RequestMapping("user")
@RestController
public class UserController {

    @Autowired
    private UserService userService;

    @PostMapping("registerUser")

    public String registerUser(@RequestBody User user) {

        //System.out.println (user);


        //System.out.println (user);

        int row = userService.registerUser (user);
        if (row > 0) {
            return "success";
        }

        return "error";
    }

    @PostMapping("findByName")
    public User findByName(@RequestBody User user) {
        //System.out.println (user);
        User byName = userService.findByName (user);

        return byName;
    }

    /**
     * 登陆验证
     * @param username
     * @param password
     * @return
     */
    @PostMapping("loginUser/{username}/{password}")
    public String loginUser(@PathVariable("username") String username, @PathVariable("password") String password) {


        User user = userService.loginUser (username, password);



        if (user == null) {

            return "failure";
        } else {

            if (user.getStatus ()!=1){
                return "error";
            }else {
                return "success";
            }


        }

    }


    @GetMapping("findByUserName/{username}")
    public User findByUserName(@PathVariable("username") String username) {

        //System.out.println (username);

        User user = userService.findByUserName (username);
        return user;
    }

    @PutMapping("isSign/{isSign}")
    public User isSign(@PathVariable("isSign") String isSign) {


        //System.out.println (isSign);

        //System.out.println (isSign);
        User user = userService.isSign (isSign);
        return user;
    }

    /**
     * 分页查询用户
     *
     * @return
     */
    @GetMapping("findPage/{page}/{size}")
    public List<User> findPage(@PathVariable("page") Integer page, @PathVariable("size") Integer size) {

        //System.out.println (page);
        //System.out.println (size);
        //List<User> userList= userService.findAll();


        List<User> userList = userService.findPage (page, size);


        return userList;
    }

    @PutMapping("stop/{uid}")
    public String stop(@PathVariable("uid")String uid){
        //System.out.println (uid);

        int row=userService.stop(uid);

        if (row>0){
            return "success";
        }else{
            return "error";
        }

    }
}
